package com.ims.controller;

import com.ims.entity.*;
import com.ims.form.CommonResponseForm;
import com.ims.form.FinancialAssistance.RetFormAdaptor;
import com.ims.form.FinancialAssistance.scholarshipinfo.*;
import com.ims.form.FinancialAssistance.stipendinfo.*;
import com.ims.form.FinancialAssistance.stipendoptions.*;
import com.ims.form.FinancialAssistance.studentloaninfo.*;
import com.ims.form.FinancialAssistance.studentoptions.*;
import com.ims.repository.ParttimeRepository;
import com.ims.repository.ScholarShipRepository;
import com.ims.repository.StipendRepository;
import com.ims.repository.StudentLoanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api
@RestController
public class FinancialAssistanceController {

    @Autowired
    StudentLoanRepository studentloanRepository;
    @Autowired
    ParttimeRepository parttimeRepository;
    @Autowired
    ScholarShipRepository scholarShipRepository;
    @Autowired
    StipendRepository stipendRepository;

    @PostMapping(value = "/studentloanoptions")
    public CommonResponseForm getStudentloanoptions(@RequestBody getStudentoptionsForm studentoptionsForm) {
        Map<String, Object> data = new HashMap<>();
        List<String> optionTimes = studentloanRepository.getOptionsForTime();
        List<String> optionRemark = studentloanRepository.getOptionsForRemark();
        data.put("optionsForTime", optionTimes);
        data.put("optionsForRemark", optionRemark);
        CommonResponseForm retForm = new CommonResponseForm(0, "已获得助学贷款过滤器选项", data);
        return retForm;
    }

    @PostMapping(value = "/studentloaninfo")
    public CommonResponseForm studentloaninfo(@RequestBody getStudentLoanForm req,
                                              BindingResult bindingResult) {
        StudentLoanEntity sl = new StudentLoanEntity();
        StudentInfoEntity si = new StudentInfoEntity();

        si.setStuclass(req.get_class());
        sl.setNote(req.getRemark());
        si.setMajor(req.getMajor());
        sl.setTime(req.getTime());
        sl.setStudent(si);
        Example<StudentLoanEntity> example = Example.of(sl);
        Pageable pages = new PageRequest(req.getCurrentPage() - 1, req.getPageSize());
        Page<StudentLoanEntity> querylist = studentloanRepository.findAll(example, pages);
        List<StudentLoanEntity> slList = querylist.getContent();
        Map<String, Object> data = new HashMap<>();
        data.put("recordQuantity", querylist.getTotalElements());
        data.put("studentLoanData", RetFormAdaptor.StudentLoanEntity_to_StudentLoanForm(slList));
        CommonResponseForm retForm = new CommonResponseForm(0, "已获得勤工助学信息", data);
        return retForm;
    }

    @PostMapping(value = "/parttimeinfo")
    public CommonResponseForm parttimeinfo(@RequestBody getMajorClassForm req,
                                           BindingResult bindingResult) {
        ParttimeEntity condition = new ParttimeEntity();
        StudentInfoEntity stud = new StudentInfoEntity();
        stud.setStuclass(req.get_class());
        stud.setMajor(req.getMajor());
        condition.setStudent(stud);
        Example<ParttimeEntity> example = Example.of(condition);
        Pageable pages = new PageRequest(req.getCurrentPage() - 1, req.getPageSize());
        Page<ParttimeEntity> querylist = parttimeRepository.findAll(example, pages);
        List<ParttimeEntity> ptList = querylist.getContent();
        Map<String, Object> data = new HashMap<>();
        data.put("recordQuantity", querylist.getTotalElements());
        data.put("partTimeData", RetFormAdaptor.ParttimeEntity_to_ParttimeForm(ptList));
        CommonResponseForm retForm = new CommonResponseForm(0, "已获得勤工助学信息", data);
        return retForm;
    }

    @PostMapping(value = "/scholarshipinfo")
    public CommonResponseForm getScholarshipInfo(@RequestBody getMajorClassForm mcForm,
                                                 BindingResult bindingResult) {
        Page<ScholarShipEntity> scp = scholarShipRepository.getscholarinfo(
                mcForm.getMajor(),
                mcForm.get_class(),
                new PageRequest(mcForm.getCurrentPage() - 1, mcForm.getPageSize())
        );
        scholarInfoReponse scr = new scholarInfoReponse(scp.getTotalElements(), scp.getContent());
        CommonResponseForm ret = new CommonResponseForm(0, "已获得奖学金信息", scr);
        return ret;
    }

    @PostMapping(value = "/stipendinfo")
    public CommonResponseForm stipendinfo(@RequestBody getStipendForm stipendForm) {
        Page<StipendEntity> se = stipendRepository.getstipendinfo(
                stipendForm.getMajor(),
                stipendForm.get_class(),
                stipendForm.getTime(),
                stipendForm.getLevel(),
                new PageRequest(stipendForm.getCurrentPage() - 1, stipendForm.getPageSize()));
        stipendInfoResponse st = new stipendInfoResponse(se.getTotalElements(), se.getContent());
        return new CommonResponseForm(0, "已返回助学金信息", st);
    }

    @PostMapping(value = "/stipendoptions")
    public CommonResponseForm stipendoptions(@RequestBody getStipendoptionsForm stipendoptionsForm) {
        Map<String, Object> ajaxJson = new HashMap<>();
        ajaxJson.put("optionsForTime", stipendRepository.getTimeOptions(
                stipendoptionsForm.getMajor(), stipendoptionsForm.get_class()));
        ajaxJson.put("optionsForLevel", stipendRepository.getLevelOptions(
                stipendoptionsForm.getMajor(), stipendoptionsForm.get_class()));
        return new CommonResponseForm(0, "已获得过滤器选项", ajaxJson);
    }
}


